package com.mengxiangnongfu.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mengxiangnongfu.constants.Constants;
import com.mengxiangnongfu.domain.DictType;
import com.mengxiangnongfu.dto.DicDataDto;
import com.mengxiangnongfu.mapper.DictDataMapper;
import com.mengxiangnongfu.vo.DataGridView;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.Arrays;
import java.util.List;

import com.mengxiangnongfu.domain.DictData;
import com.mengxiangnongfu.service.DictDataService;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Service
@Transactional(rollbackFor = Exception.class)
public class DictDataServiceImpl implements DictDataService {

    @Resource
    private DictDataMapper dictDataMapper;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @Override
    public DataGridView loadDictDataByPage(DicDataDto dicDataDto) {
        Page<DictData> page = new Page<>(dicDataDto.getPageNum(), dicDataDto.getPageSize());
        QueryWrapper<DictData> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(dicDataDto.getDictType()), DictType.COL_DICT_TYPE, dicDataDto.getDictType());
        queryWrapper.like(StringUtils.isNotBlank(dicDataDto.getDictLabel()), DictData.COL_DICT_LABEL, dicDataDto.getDictLabel());
        queryWrapper.eq(StringUtils.isNotBlank(dicDataDto.getStatus()), DictData.COL_STATUS, dicDataDto.getStatus());
        queryWrapper.orderByAsc(DictData.COL_DICT_SORT);
        this.dictDataMapper.selectPage(page, queryWrapper);
        return new DataGridView(page.getTotal(), page.getRecords());
    }

    @Override
    public Integer insertDicData(DicDataDto dicDataDto) {
        DictData dictData = new DictData();
        BeanUtil.copyProperties(dicDataDto, dictData);
        dictData.setCreateBy(dicDataDto.getSimpleUser().getUserName());
        dictData.setCreateTime(DateUtil.date());
        return this.dictDataMapper.insert(dictData);
    }

    @Override
    public DictData loadDictDataById(Serializable id) {
        return this.dictDataMapper.selectById(id);
    }

    @Override
    public Integer modifyDicData(DicDataDto dicDataDto) {
        DictData dictData = new DictData();
        BeanUtil.copyProperties(dicDataDto, dictData);
        dictData.setUpdateBy(dicDataDto.getSimpleUser().getUserName());
        dictData.setUpdateTime(DateUtil.date());
        return this.dictDataMapper.updateById(dictData);
    }

    @Override
    public Integer removeDicData(Long[] ids) {
        List<Long> arr = Arrays.asList(ids);
        if (null != arr && arr.size() > 0) {
            return this.dictDataMapper.deleteBatchIds(arr);
        } else {
            return -1;
        }

    }

    @Override
    public DataGridView loadDictDataByType(String type) {
        String value = stringRedisTemplate.opsForValue().get(Constants.DICT_REDIS_PROFIX + type);
        List<DictData> dictDataList = JSON.parseArray(value, DictData.class);
        return new DataGridView((long) dictDataList.size(), dictDataList);
    }
}
